<?php
/**
 * 搜索
 * @link https://www.leadshop.vip/
 * @copyright Copyright ©2020-2021 浙江禾成云计算有限公司
 */

namespace leadmall\api;

use leadmall\Map;
use store\models\Store;
use system\api\AccountController as AccountModules;
use Yii;
use yii\web\ForbiddenHttpException;
use yii\data\ActiveDataProvider;

class AccountController extends AccountModules implements Map
{
    public $modelClass = 'system\models\Account';
    public $modelClass2 = 'system\models\Roles';
    public $modelClass3 = 'store\models\Store';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['create']);
        unset($actions['index']);
        return $actions;
    }


    public function actionIndex()
    {
        //获取头部信息
        $modelClass = $this->modelClass;
        $modelClass2 = $this->modelClass2;
        $headers = Yii::$app->getRequest()->getHeaders();
        //获取分页信息
        $pageSize = $headers->get('X-Pagination-Per-Page') ?? 20;
        $where=['is_deleted'=>0];
        $orderBy = ['created_time' => SORT_DESC];
        $accouts= $modelClass::find()
            ->where($where)
            ->groupBy('id')
            ->orderBy($orderBy)
            ->asArray()->all();
        foreach ($accouts as $kb=>$veb){
            $vidba =[];
            if($veb['roles']) {
                if($veb['roles']!='all') {
                    $visitArray = json_decode($veb['roles'], true);
                    $list = $modelClass2::find()->select('id,name')->where(['is_deleted' => 0])
                        ->andWhere(['in', 'id', $visitArray])->all();
                    foreach ($list as $kbc => $ve) {
                        $vidba[$kbc]['id'] = $ve['id'];
                        $vidba[$kbc]['label'] = $ve['name'];
                    }
                    $accouts[$kb]['roles'] = $vidba;
                }else{
                   $vidba[0]['id'] = '0';
                    $vidba[0]['label'] = '系统管理员';
                    $accouts[$kb]['roles'] = $vidba;
                }
            }
            $resultd = Store::find()
                ->where(['id' => $veb['store_id']])
                ->andWhere(['deleted' => 0])
                ->asArray()
                ->one();
            if(!$resultd){
                $store_name = '門店已刪除';
            }else{
                $store_name =  $resultd['store_name'];
            }
            $accouts[$kb]['store_name'] = $store_name;
        }
        return $accouts;
//        $data = new ActiveDataProvider(
//            [
//                'query'      => $accouts,
//                'pagination' => ['pageSize' => $pageSize, 'validatePage' => false],
//            ]
//        );
//        return $data;
    }


//    public function actionCreate()
//    {
//        return $this->actionList();
//    }

}
